package com.example.sherlockzhong.plush_v2.Utils;

import android.graphics.Bitmap;
import android.os.Environment;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by SherlockZhong on 9/11/14.
 */
public class StorageUtils {

    public static File baseDir = new File(Environment.getExternalStorageDirectory() + "/ZhiLiao/");
    public static File dataDir = new File(baseDir + "/data/");
    public static File imageDir = new File(baseDir + "/image/");
    public static File audioDir = new File(baseDir + "/audio/");

    public static boolean saveAsFile(String filename, String content) {
        String en = Environment.getExternalStorageState();
        if (en.equals(Environment.MEDIA_MOUNTED)) {
            try {
                dataDir.mkdirs();
                File file = new File(dataDir, filename);
                OutputStream out = new FileOutputStream(file);
                out.write(content.getBytes());
                out.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean saveAsFile(String filename, InputStream content) {
        String en = Environment.getExternalStorageState();
        if (en.equals(Environment.MEDIA_MOUNTED)) {
            try {
                dataDir.mkdirs();
                File file = new File(dataDir, filename);
                OutputStream out = new FileOutputStream(file);
                byte[] buff = new byte[1024];
                int hasRead = 0;
                while ((hasRead = content.read(buff)) > 0) {
                    out.write(buff, 0, hasRead);
                }
                out.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }


    public static boolean saveAsImage(String filename, Bitmap bitmap) {
        String en = Environment.getExternalStorageState();
        if (en.equals(Environment.MEDIA_MOUNTED)) {
            try {
                if (!fileExists("/image/" + filename)) {
                    imageDir.mkdirs();
                    File file = new File(imageDir, filename);
                    OutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.close();
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }


//        try {
//            FileOutputStream os = new FileOutputStream(imageDir);
//            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
//            os.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        Bitmap bitmap1 = BitmapFactory.decodeStream(is);
//        FileOutputStream output = new FileOutputStream(String.format("%s" + "smartCamplus/Image/" + id + ".jpg", Constant.SAVE_WHERE));//输出的路径和名字
//        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, output);
//        is.close();
//        output.close();

    }

//    public static InputStream retrieveStreamFramFile(Activity app,String filename){
//        try{
//            FileInputStream fis = app.openFileInput(filename);
//            byte[] buff = new byte[1024];
//            int hasRead = 0;
//            while ((hasRead = fis.read(buff))>0){
//
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }

    public static String retrieveFromFile(String filename) {
        String result = null;
        try {
            File file = new File(dataDir + "/" + filename);
            if (!fileExists("/data/" + filename)) {
                return (new JSONArray().toString());
            }
            InputStream in = null;
            in = new FileInputStream(file);
            int length = (int) file.length();
            byte[] temp = new byte[length];
            in.read(temp, 0, length);
            in.close();
            result = EncodingUtils.getString(temp, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean fileExists(String filepath) {
        try {
            File f = new File(baseDir, filepath);
            return f.exists();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean removeFile(String filepath) {
        try {
            if (fileExists(filepath)) {
                File f = new File(baseDir, filepath);
                return f.delete();
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void removeDirectory(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }
        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }
            for (int i = 0; i < childFiles.length; i++) {
                removeDirectory(childFiles[i]);
            }
            file.delete();
        }
    }

}
